/*  ----------------------------------------------------------------------------
*	This file is part of the AS3This ActionScript 3 library.
*	Author: Steve Jones for G-RAFF <http://www.g-raff.com/>
*	Copyright (c) 2009 raffWorks Ltd.
*	----------------------------------------------------------------------------
*	Please see the notice at the bottom of the code before using or modifying this class.
*/
package com.as3this.core
{
	import com.as3this.events.AddThisEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class ServiceMetadata extends EventDispatcher
	{
		public static const ENDPOINT_URL	:String = "http://cache.addthiscdn.com/services/v<major>/sharing.<lang>.xml";
		private var _lang					:String;
		private var _major					:String;
		private var _services				:Array = new Array();
		
		public function ServiceMetadata(lang:String = "en", major:int = 0)
		{
			_lang = lang;
			_major = major.toString();
		}
		
		public function load() : void
		{
			var metaURL:String = ENDPOINT_URL.replace("<lang>", _lang);
			metaURL = metaURL.replace("<major>", _major);
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE, completeHandler);
			urlLoader.load(new URLRequest(metaURL));
		}
		
		private function completeHandler(event:Event) : void
		{
			// clean up - remove event listener
			var urlLoader:URLLoader = event.target as URLLoader;
			urlLoader.removeEventListener(Event.COMPLETE, completeHandler);
			
			
			var xml:XML = XML(event.target.data);
			var servicesXMLList:XMLList = xml..services.service;
			
			// loop through and create the service value objects
			for each(var serviceXML:XML in servicesXMLList)
			{
				var code	:String = serviceXML.@code.toString();
				var name	:String = serviceXML.name.toString();
				var iconURL	:String = serviceXML.icon.toString();
				
				// wrap with a service VO for agreed getters
				_services.push( new Service(code, name, iconURL) );
			}
			
			dispatchEvent(new AddThisEvent(AddThisEvent.META_DATA, _services));
		}
		
		public function getMetaData(serviceCode:String) : Service
		{
			for (var i:uint = 0; i < _services.length; i++)
			{
				if (_services[i].code == serviceCode) return _services[i];
			}
			
			return null;		
		}
		
		public function get services() : Array
		{
			return _services;
		}
	}
}
/*
 *    AS3This is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    AS3This is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with AS3This.  If not, see <http://www.gnu.org/licenses/>.
 */